package com.waijiaojun.tpo.web;

import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.OperationGuidePageMarkService;
import com.waijiaojun.tpo.service.UserService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.utils.UserUtil;
import com.waijiaojun.tpo.vo.IndexData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/operation-guide")
public class OperationGuideController {

    private static Logger logger = LoggerFactory.getLogger(OperationGuideController.class);

    @Autowired
    private OperationGuidePageMarkService operationGuidePageMarkService;

    @RequestMapping("search/{pageName}")
    @ResponseBody
    public RestResponse search(@PathVariable("pageName") String pageName) {
        RestResponse restResponse = null;
        try {
            // 打印入参
            logger.info(Constants.prefix_in + " pageName=" + pageName);

            String openid = UserUtil.getCurrentUserOpenId();
            if (null == openid) {
                throw new RestClientException("未登录");
            }
            if(StringUtils.isBlank(pageName)){
                throw new RestClientException("参数无效：pageName不能为空");
            }
            int isShowMark = operationGuidePageMarkService.showMarkForUser(openid);// 0不显示引导页；1 显示引导页
            restResponse =  RestResponse.getSuccessResponse();
            restResponse.setData(isShowMark);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        return restResponse;
    }

    @RequestMapping("save/{pageName}")
    @ResponseBody
    public RestResponse save(@PathVariable("pageName") String pageName) {
        RestResponse restResponse = RestResponse.getSuccessResponse();
        try {
            // 打印入参
            logger.info(Constants.prefix_in + " pageName=" + pageName);

            String openid = UserUtil.getCurrentUserOpenId();
            if (null == openid) {
                throw new RestClientException("未登录");
            }
            if(StringUtils.isBlank(pageName)){
                throw new RestClientException("参数无效：pageName不能为空");
            }
            operationGuidePageMarkService.saveUserMark(openid,pageName);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        return restResponse;
    }


}
